<?php

class Rapleaf {

  var $base_url;
  var $api_key;

  function Rapleaf($api_key, $url = 'http://www.rapleaf.com') {
    $this->api_key = $api_key;
    $this->base_url = $url;
  }

# Handles creation of the LookupRequest object
# Returns a RapleafResponse
  function lookup($params) {
    $request = new LookupRequest($params);
    $result = $request->submitRequest($this->api_key, $this->base_url);
    return $result;
  }

# Handles creation of the RatingRequest object
# Returns a RapleafResponse
  function rate($params) {
    $request = new RatingRequest($params);
    $result = $request->submitRequest($this->api_key, $this->base_url);
    return $result;
  }
  }

# Set up and submit a request to lookup a Rapleaf profile
# see http://www.rapleaf.com/apidoc/v1/lookup for details about parameters
class LookupRequest extends Rapleaf {
  var $params = array();

  function LookupRequest($params) {
    $this->params = $params;
  }

  function submitRequest($api_key, $base_url) {
    $url = $base_url.'/API/lookup';
    $data = 'api_key='.$api_key;
    foreach ($this->params as $key=>$val) {
      if (is_array($val) && count($val) > 0) {
	$data .= '&'.$key.'='.implode(',', $val);
      } else if (!is_null($val)) {
	$data .= '&'.$key.'='.$val;
      }
    }
    if ($responseXML = send_post_request($url,$data)) {
      return new RapleafResponse($responseXML);
    } else {
      return null;
    }
  }
}

# Set up and submit a request to rate someone
# see http://www.rapleaf.com/apidoc/v1/report_rating for details about parameters
class RatingRequest extends Rapleaf {
  var $params = array();

  function RatingRequest($params = array()) {
    $this->params = $params;
  }
  
  function submitRequest($api_key, $base_url) {
    $url = $base_url.'/API/report_rating';
    $data = '<request><API_key>'.$api_key.'</API_key><query>';
    foreach ($this->params as $key=>$val) {
      if (is_array($val) && count($val) > 0) {
	$data .= '<'.$key.'>'.implode(',', $val).'</'.$key.'>';
      } else if (!is_null($val)) {
	$data .= '<'.$key.'>'.$val.'</'.$key.'>';
      }
    }
    $data .= '</query></request>';

    if ($responseXML = send_post_request($url,$data,TRUE)) {
      return new RapleafResponse($responseXML);
    } else {
      return null;
    }
  }
}

# Stores the results of a *Request
#  (same response format for LookupRequest and RatingRequest)
#  see examples.php for an example of a RapleafResponse
#  see http://www.rapleaf.com/apidoc/v1/report_rating or
#   http://www.rapleaf.com/apidoc/v1/lookup for details about response messages
class RapleafResponse{
  function RapleafResponse($xml) {
    $this->xml_response = $xml;
    $p = xml_parser_create();
    xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
    xml_parse_into_struct($p, $xml, $vals, $index);
    xml_parser_free($p);
    foreach ($index as $key=>$val) {
      if (count($val) == 1) {
	$tag = $vals[$val[0]];
	if ($tag['type'] == 'complete')
	  $this->{$tag['tag']} = $tag['value'];
      }
    }
  }

  function failed() {
    if ('failed' == $this->status)
      return true;
    return false;
  }

}

# Helper function used to send requests
function send_post_request($url, $post_data, $is_XML=FALSE) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  if ($is_XML)
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-POST_DATA_FORMAT: xml"));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

?>
